package cn.edu.xaut.mybatisers.mall.Controller;

import cn.edu.xaut.mybatisers.mall.Entity.AReturnDataStruct;
import cn.edu.xaut.mybatisers.mall.Entity.Commodity;
import cn.edu.xaut.mybatisers.mall.Entity.Favourite;
import cn.edu.xaut.mybatisers.mall.Exception.ExceptionEntity.ParametersIncompleteException;
import cn.edu.xaut.mybatisers.mall.Service.FavouriteService;
import cn.edu.xaut.mybatisers.mall.ServiceImp.FavouriteServiceImp;
import cn.edu.xaut.mybatisers.mall.tool.AReturnStructFactory;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/favorite")
public class FavouriteController {
    @Autowired
    FavouriteServiceImp favouriteServiceImp;

    @ApiOperation("增加收藏数量")
    @RequestMapping(value = "/incCommodityNumber",method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct incCommodityNumber(Favourite favourite, HttpSession session) throws SQLException, ParametersIncompleteException {
        if (favourite.getCommodity_id() <= 0){
            throw new ParametersIncompleteException("商品id不完整");
        }

        favourite.setConsumer_id((Integer)session.getAttribute("consumer_id"));
        favouriteServiceImp.incF(favourite);
        AReturnDataStruct aReturnDataStruct = new AReturnDataStruct();
        aReturnDataStruct.setResult(true);
        aReturnDataStruct.setCode(200);
        aReturnDataStruct.setMessage("收藏成功");
        return aReturnDataStruct;
    }

    @ApiOperation("减少收藏数量")
    @RequestMapping(value = "/subCommodityNumber",method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct subCommodityNumber(Favourite favourite, HttpSession session) throws SQLException, ParametersIncompleteException {
        if (favourite.getCommodity_id() <= 0){
            throw new ParametersIncompleteException("商品id不完整");
        }
        favourite.setConsumer_id((Integer)session.getAttribute("consumer_id"));
        favouriteServiceImp.subF(favourite);
        AReturnDataStruct aReturnDataStruct = new AReturnDataStruct();
        aReturnDataStruct.setResult(true);
        aReturnDataStruct.setCode(200);
        aReturnDataStruct.setMessage("取消收藏");
        return aReturnDataStruct;
    }


    @ApiOperation("查询收藏商品id")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    @ResponseBody
    public AReturnDataStruct getAllFavorite(HttpSession session) throws SQLException {
        List<Integer> allFavouriteIds = favouriteServiceImp.getAllFavourite(session);
        return AReturnStructFactory.returnDataStructFactory(true, "查询成功", 200, allFavouriteIds);
    }

}
